<template>
  <section>
    <el-form :inline="true" :model="listQuery" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="listQuery.username" placeholder="Username" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="users" border fit highlight-current-row style="width: 100%">
      <el-table-column width="80" align="center" label="ID">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180" align="center" label="Username">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180" align="center" label="Phone Number">
        <template slot-scope="scope">
          <span>{{ scope.row.cell_phone_number }}</span>
        </template>
      </el-table-column>

      <el-table-column width="300" align="center" label="Models">
        <template slot-scope="scope">
          <el-tag v-for="(model, index) in scope.row.models" :key="index" :type="model | statusFilter">
            {{ model }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column width="180" align="center" label="Date">
        <template slot-scope="scope">
          <span>{{ scope.row.create_date | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="150" align="center" label="Actions">
        <template slot-scope="scope">
          <el-button size="mini" @click="onViewUserFaces(scope.row.id)">人脸</el-button>
          <el-button size="mini" type="danger" @click="onRemoveUser(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getUsers" />

    <el-dialog title="人脸模板" :visible.sync="dialogFacesVisible" width="40%">
      <el-carousel trigger="click" :autoplay="false">
        <el-carousel-item v-for="(photoUrl, idx) in photoUrls" :key="idx" style="text-align: center;">
          <!-- <h3 class="small">{{ photoUrl }}</h3> -->
          <el-image style="width: 400px; height: 300px" :src="photoUrl" />
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </section>
</template>

<script>
import { fetchUsers, deleteUser } from '@/api/user'
import { fetchPhotoNames } from '@/api/photo'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Users',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        FaceNet: 'success',
        CosFace: 'info',
        ArcFace: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      users: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        username: null
      },
      dialogFacesVisible: false,
      photoUrls: []
    }
  },
  created() {
    this.getUsers()
  },
  mounted() {},
  methods: {
    getUsers() {
      this.listLoading = true
      fetchUsers(this.listQuery).then(res => {
        // console.log(res)
        this.total = res.data._meta.total_items
        this.users = res.data.items
        this.listLoading = false
      }).catch(error => {
        this.$message.error(error.message)
        this.listLoading = false
      })
    },
    onQuery() {
      this.getUsers()
    },
    onViewUserFaces(userId) {
      this.dialogFacesVisible = true
      this.getPhotos(userId)
    },
    onRemoveUser(userId) {
      this.$confirm('此操作将永久删除该数据, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        deleteUser(userId).then(res => {
          this.getUsers()
        }).catch(error => {
          this.$message.error(error.message)
          this.listLoading = false
        })
      })
    },
    getPhotos(userId) {
      this.photoUrls = []
      fetchPhotoNames(userId).then(res => {
        if (res.data) {
          for (const photoName of res.data) {
            const photoUrl = process.env.VUE_APP_BASE_API + '/photo/' + photoName
            this.photoUrls.push(photoUrl)
          }
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-tag+.el-tag {
  margin-left: 10px;
}
</style>
